#include "AnalysisTool.h"

#include <osg/NodeVisitor>
#include <osgGA/EventVisitor>
#include <osgViewer/View>

CAnalysisTool::CAnalysisTool(osgEarth::MapNode* pMapNode) 
	: m_opMapNode(pMapNode)
{
	setNumChildrenRequiringEventTraversal(1);
}

CAnalysisTool::~CAnalysisTool()
{

}

void CAnalysisTool::SetEnabled(bool enabled)
{
	if (enabled)
	{
		setNumChildrenRequiringEventTraversal(1);
	} else {
		setNumChildrenRequiringEventTraversal(0);
	}
}

bool CAnalysisTool::IsEnabled()
{
	return (getNumChildrenRequiringEventTraversal() == 1);
}

void CAnalysisTool::traverse(osg::NodeVisitor& nv)
{
	if (nv.getVisitorType() == osg::NodeVisitor::EVENT_VISITOR)
	{
		osgGA::EventVisitor* ev = static_cast<osgGA::EventVisitor*>(&nv);
		for(osgGA::EventQueue::Events::iterator itr = ev->getEvents().begin();
			itr != ev->getEvents().end();
			++itr)
		{
			osgGA::GUIEventAdapter* ea = dynamic_cast<osgGA::GUIEventAdapter*>(itr->get());
			if ( ea && handle(*ea, *(ev->getActionAdapter())))
				ea->setHandled(true);
		}
	}
	IAnalysisTool::traverse(nv);
}

bool CAnalysisTool::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
	if (ea.getHandled()) return false;

	osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
	if (!view) return false;
	
	return false;
}

void CAnalysisTool::setMapNode(osgEarth::MapNode* mapNode)
{
	m_opMapNode = mapNode;
}

osgEarth::MapNode* CAnalysisTool::getMapNode()
{
	return m_opMapNode.get();
}
